//
//  ZLHomeViewController.m
//  ZLExamination
//
//  Created by Zheng on 2017/8/9.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLHomeViewController.h"

#import "ZLCourseLerningDetailViewController.h"

#import "ZLHomeTopPortCell.h"
#import "ZLHomeSectionTitleView.h"
#import "ZLHomeTopicCell.h"

#import "ZLTogetherLearnHomeViewController.h"

#import "ZLPublicNoticeTableViewController.h"
#import "ZLExciseResultViewController.h"
#import "ZLExciseBeginViewController.h"
#import "ZLPublicNoticeDetailViewController.h"

#import "ZLHomeModel.h"

static NSString *kZLHomeTopPortCell_D = @"kZLHomeTopPortCell_D";
static NSString *kZLHomeTopicCell_ID = @"kZLHomeTopicCell_ID";

@interface ZLHomeViewController ()

@property (nonatomic, strong) ZLHomeModel *model;

@end

@implementation ZLHomeViewController

#pragma mark - 点击事件

- (void)clickAction:(ZLHomeSectionTitleView *)titleView {
    if ([titleView.titleLbl.text isEqualToString:@"公告通知"]) {
        kCX_LOG(@"公告通知");
        ZLPublicNoticeTableViewController *publicNoticeVC = [[ZLPublicNoticeTableViewController alloc] init];
        publicNoticeVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:publicNoticeVC animated:YES];
        
    } else {
        
        ZLTogetherLearnHomeViewController *togetherVC = [[ZLTogetherLearnHomeViewController alloc] init];
        togetherVC.hidesBottomBarWhenPushed = YES;

        if ([titleView.titleLbl.text isEqualToString:@"入矫教育学习"]) {
            kCX_LOG(@"入矫教育学习全部");
            togetherVC.navigationItem.title = @"入矫教育学习";
            togetherVC.type = @"1";
           
        } else if ([titleView.titleLbl.text isEqualToString:@"矫正教育学习"]) {
            kCX_LOG(@"矫正教育学习全部");
            togetherVC.navigationItem.title = @"入矫教育学习";
            togetherVC.type = @"2";

        } else if ([titleView.titleLbl.text isEqualToString:@"解矫教育学习"]) {
            kCX_LOG(@"解矫教育学习全部");
            togetherVC.navigationItem.title = @"入矫教育学习";
            togetherVC.type = @"3";
        }
        [self.navigationController pushViewController:togetherVC animated:YES];
    }
}

- (void)btnClickActionWithStyle:(ZLHomeTopPortCellActionStyle)style {
    
    ZLTogetherLearnHomeViewController *togetherVC = [[ZLTogetherLearnHomeViewController alloc] init];
    togetherVC.hidesBottomBarWhenPushed = YES;
    
    switch (style) {
        case ZLHomeTopPortCellActionStyleFirstKind: {
            kCX_LOG(@"入矫教育学习");
            togetherVC.navigationItem.title = @"入矫教育学习";
            togetherVC.type = @"1";
        }
            break;
        case ZLHomeTopPortCellActionStyleSecondeKind: {
            kCX_LOG(@"常规教育学习");
            togetherVC.navigationItem.title = @"常规教育学习";
            togetherVC.type = @"2";
        }
            break;
        case ZLHomeTopPortCellActionStyleThirdKind: {
            kCX_LOG(@"解矫教育学习");
            togetherVC.navigationItem.title = @"解矫教育学习";
            togetherVC.type = @"3";
        }
            break;
        case ZLHomeTopPortCellActionStyleLearnning: {
            kCX_LOG(@"正在学习");
            togetherVC.navigationItem.title = @"正在学习";
            togetherVC.isLearning = YES;
        }
            break;
    }
    
    [self.navigationController pushViewController:togetherVC animated:YES];
    
}

#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"首页";
    
    [self.myTableView registerClass:[ZLHomeTopPortCell class] forCellReuseIdentifier:kZLHomeTopPortCell_D];
    
    UIImageView *headerView = [[UIImageView alloc] init];
    headerView.frame = (CGRect){0,0,kSCREEN_WIDTH,kCX_Scale(140)};
    headerView.image = kIMAGE(@"home_top_b");
    self.myTableView.tableHeaderView = headerView;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}

#pragma mark - 加载数据
- (void)loadData {
    
    [[YCRequestDataService sharedManager] Post:@"index/index/lists" parameters:nil success:^(id response) {
        if (response[@"error_code"]) {
            NSLog(@"%@",response[@"error_msg"]);
        } else {
            _model = [[ZLHomeModel alloc] initWithDataDic:response[@"response_data"][@"lists"]];
            
            [self.myTableView reloadData];
        }
    } failure:^{
        
    }];
}


#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1+_model.sectionNumbers;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    } else {
        return [_model.lists[section-1] count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        __weak typeof(self) weakSelf = self;
        ZLHomeTopPortCell *cell = [tableView dequeueReusableCellWithIdentifier:kZLHomeTopPortCell_D forIndexPath:indexPath];
        cell.clickActionBlock = ^(ZLHomeTopPortCellActionStyle style) {
            [weakSelf btnClickActionWithStyle:style];
        };
        return cell;
    } else {
        
        if([_model.listType[indexPath.section-1] isEqual:@0]) {
            UITableViewCell *cell = [[UITableViewCell alloc] init];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.font = kMyFont(11);
            cell.textLabel.textColor = [UIColor grayColor];
            
            ZLHomeNoticeModel *model = _model.lists[indexPath.section-1][indexPath.row];
//            cell.textLabel.text = [NSString stringWithFormat:@"·%@   %@：%@",[YCRequestDataService timeintervalFormatYMD:@(model.add_time.integerValue)],model.title,model.content];

            NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[[NSString stringWithFormat:@"·%@   %@：%@",[YCRequestDataService timeintervalFormatYMD:@(model.add_time.integerValue)],model.title,model.content] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
            //
            cell.textLabel.attributedText = attrStr;
            
            return cell;
        } else {
            ZLHomeTopicCell *cell = [[ZLHomeTopicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            ZLHomeCourseLearningModel *model = _model.lists[indexPath.section-1][indexPath.row];
            cell.model = model;
            return cell;
        }
        
    }
}

#pragma mark - tableview delegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return nil;
    } else {
        NSArray *titles = @[@"公告通知",@"入矫教育学习",@"矫正教育学习",@"解矫教育学习"];
        NSString *title = titles[([_model.listType[section-1] integerValue])];
        ZLHomeSectionTitleView *titleView = [[ZLHomeSectionTitleView alloc] init];
        titleView.frame = (CGRect){0,0,kSCREEN_WIDTH,40};
        titleView.titleLbl.text = title;
        [titleView addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        return titleView;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return kSCREEN_WIDTH/4;
    } else {
        if([_model.listType[indexPath.section-1] isEqual:@0]) {
            return 35;
        } else {
            return 70;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.001;
    } else {
        return 40;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section != 0) {
        if([_model.listType[indexPath.section-1] isEqual:@0]) {
            kCX_LOG(@"最新通知%ld",indexPath.row);
            ZLHomeNoticeModel *model = _model.lists[indexPath.section-1][indexPath.row];
            
            ZLPublicNoticeDetailViewController *detailVC = [[ZLPublicNoticeDetailViewController alloc] init];
            detailVC.n_id = model.x_id;
            detailVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:detailVC animated:YES];
            
            
        } else {
            kCX_LOG(@"课程学习%ld-%ld",indexPath.section,indexPath.row);
            ZLCourseLerningDetailViewController *cldVC = [[ZLCourseLerningDetailViewController alloc] init];
            ZLHomeCourseLearningModel *model = _model.lists[indexPath.section-1][indexPath.row];
            cldVC.navigationItem.title = model.title;
            cldVC.curriculum_id = model.u_id;
            cldVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:cldVC animated:YES];
        }
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section > 1) {
        if(![_model.listType[indexPath.section-1] isEqual:@0]) {
            if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                [cell setLayoutMargins:UIEdgeInsetsZero];
            }
            if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
                [cell setSeparatorInset:UIEdgeInsetsZero];
            }
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
